using System;
using System.Collections;
using System.Data;
using System.Net;
using System.Text.RegularExpressions;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using PHSRAG.Insight21.CDS;
using PHSRAG.Insight21.Common.Pages;
using PHSRAG.Insight21.Context;
using PHSRAG.Insight21.FormsManagement;
using PHSRAG.Insight21.FormsManagement.Serialization;
using PHSRAG.Insight21.InsightWorkFlow;
using PHSRAG.Insight21.Policy;
using PHSRAG.Security;
using PHSRAG.Utility;
using Workflow.NET;
using Workflow.NET.Engine;
using Workflow.NET.Interfaces;
using Workflow.NET.Storage;
using InsightWF = PHSRAG.Insight21.InsightWorkFlow;
using InsightUtility = PHSRAG.Insight21.InsightUtilities ;
using SkeltaWorkflow = Workflow.NET;

namespace PHSRAG.Insight21.Humans
{
    /// <summary>
    /// This page gets embeded in Skelta activity control. Skelta has a concept called custom document view.
    /// This is a hook where we can show application specific information. In Skelta each approval process
    /// needs to have custom document property set which is pointing to this page URL and this page gets displayed
    /// at that particular approval step. 
    /// </summary>
    public partial class WorkFlowRevNCert : BasePage
    {
        #region Constants		

        private const string Accept = "Accept";
        private const string AcceptScript = "<script>parent.document.getElementsByTagName('input')['btnReject'].disabled=true;parent.document.getElementsByTagName('input')['btnApprove'].disabled=false;</script>";
        private const string BoldEnd = "</B>";
        private const string Checked = "CHECKED";
        private const string Crlf = "\r\n";
        private const string DisableScript = "<script>parent.document.getElementsByTagName('input')['btnReject'].disabled=true;parent.document.getElementsByTagName('input')['btnApprove'].disabled=true;</script>";
        private const string Domain = "Domain";
        private const string DomainName = "Partners";
        private const string ElementType = "TYPE=";
        private const string EnableScript = "<script>parent.document.getElementsByTagName('input')['btnReject'].disabled=false;parent.document.getElementsByTagName('input')['btnApprove'].disabled=false;</script>";
        private const string EndTagUpper = "</TABLE>";
        private const string Heading = "HEADING";
        private const string HiddenValueFiled = "hidValue";
        private const string Input = "INPUT";
        private const string InsightLogo = "InsightLogo";
        private const string LabelStart = "<LABEL";
        private const string NotApplicable = "NA";
        private const string PPTypeAdverseEvent = "AdverseEvent";
        private const string PPTypeAmendment = "Amendment";
        private const string PPTypeApplication = "Application";
        private const string PPTypeContinuingReview = "ContinuingReview";
        private const string PPTypeOtherEvent = "OtherEvent";
        private const string Reject = "Reject";
        private const string RejectScript = "<script>parent.document.getElementsByTagName('input')['btnReject'].disabled=false;parent.document.getElementsByTagName('input')['btnApprove'].disabled=true;</script>";
        private const string StartTag1Upper = "<TABLE ID=OUTPUT_";
        private const string StartTag2Upper = "<TABLE ID=\"OUTPUT_";
        private const string SubmitterRejectScript = "<script>parent.document.getElementsByTagName('input')['btnReject'].disabled=false;parent.document.getElementsByTagName('input')['btnApprove'].disabled=true;parent.document.getElementsByTagName('input')['btnReject'].value = 'Resubmit'</script>";
        private const string Tab = "\t";
        private const string TagEnd = "</";
        private const string TagEndChar = ">";
        private const string TDEnd = "</TD>";
        private const string TDStart = "<TD>";
        private const string TextType = "TEXT";
        private const string WorkFlowName = "EIRBWF";

        #endregion

        private ICachePolicy _cachePolicy;
        private ProtocolProcess _pp;
        private UserContext _userContext;

        /// <summary>
        /// Gets the protocol .If it's not already in the cache, it loads it up based on the protocolId passed to the page, and caches the object
        /// </summary>
        private Protocol Protocol
        {
            get { return _cachePolicy.GetCachedObject(Protocol.ProtocolKey) as Protocol; }
        }

        #region Events

        /// <summary>
        /// Loads the page. Loads page elements depends on the role from where it gets called. 
        /// Pulls data from incoming cache.
        /// </summary>
        /// <param name="sender">Control which raised the event</param>
        /// <param name="e">Arguments associated with the event</param>
        protected void OnPageLoad(object sender, EventArgs e)
        {
            authenticate.Attributes.Add("onClick", "document.wfRevNCertForm.hidValue.value = document.getElementById('outer').innerHTML;");
            _pp = (ProtocolProcess) _cachePolicy.GetCachedObject(Keys.ProtocolProcessKey);
            string role = Request.QueryString["RoleType"];
            if (!Page.IsPostBack)
            {
                annunciator.Message = "";
                HumansWorkFlow wfHumans = new HumansWorkFlow(_cachePolicy);
                string ppType = string.Empty;
                LoadPageElements(ppType, role);
            }
        }

        /// <summary>
        /// Gets called when Accept checkbox changes the state. 
        /// This event is wired from all accept checkboxes which are present on different panels.
        /// SEndr's Id is checked and accordingly different actions are taken.
        /// </summary>
        /// <param name="sender">Control which raised the event</param>
        /// <param name="e">Arguments associated with the event</param>
        protected void OnAcceptCheckedChanged(object sender, EventArgs e)
        {
            string controlName = ((CheckBox) sender).ID;
            userName.Text = password.Text = annunciator.Message = string.Empty;
            ClientScript.RegisterStartupScript(GetType(), "disable", DisableScript);
            switch (controlName.ToLower())
            {
                case "pichkaccept":
                    if (piChkAccept.Checked)
                    {
                        piChkDecline.Checked = false;
                        piCertification.Visible = true;
                        authentication.Visible = true;
                        piReview.Visible = true;
                        checkBoxSelected.Value = Accept;
                    }
                    else
                    {
                        piChkDecline.Checked = true;
                        piCertification.Visible = false;
                        authentication.Visible = false;
                        piReview.Visible = true;
                        checkBoxSelected.Value = Reject;
                    }
                    break;
                case "animalspichkaccept":
                    if (animalspiChkAccept.Checked)
                    {
                        animalspiChkDecline.Checked = false;
                        animalsPICertification.Visible = true;
                        authentication.Visible = true;
                        animalsPIReview.Visible = true;
                        checkBoxSelected.Value = Accept;
                    }
                    else
                    {
                        animalspiChkDecline.Checked = true;
                        animalsPICertification.Visible = false;
                        authentication.Visible = false;
                        animalsPIReview.Visible = true;
                        checkBoxSelected.Value = Reject;
                    }
                    break;
                case "updatedsschkaccept":
                    if (updatedssChkAccept.Checked)
                    {
                        updatedssChkDecline.Checked = false;
                        ssCertification.Visible = true;
                        authentication.Visible = true;
                        updatedSSReview.Visible = true;
                        checkBoxSelected.Value = Accept;
                    }
                    else
                    {
                        updatedssChkDecline.Checked = true;
                        ssCertification.Visible = false;
                        authentication.Visible = true;
                        updatedSSReview.Visible = true;
                        checkBoxSelected.Value = Reject;
                    }
                    break;
                case "updatedanimalssschkaccept":
                    if (updatedAnimalsssChkAccept.Checked)
                    {
                        updatedAnimalsssChkDecline.Checked = false;
                        animalsssCertification.Visible = true;
                        authentication.Visible = true;
                        updatedAnimalsssReview.Visible = true;
                        checkBoxSelected.Value = Accept;
                    }
                    else
                    {
                        updatedAnimalsssChkDecline.Checked = false;
                        updatedAnimalsssChkAccept.Checked = false;
                        animalsssCertification.Visible = false;
                        authentication.Visible = true;
                        updatedAnimalsssReview.Visible = true;
                        checkBoxSelected.Value = Reject;
                    }
                    break;
                case "sschkaccept":
                    if (ssChkAccept.Checked)
                    {
                        ssChkDecline.Checked = false;
                        ssCertification.Visible = false;
                        authentication.Visible = true;
                        ssReview.Visible = true;
                        checkBoxSelected.Value = Accept;
                    }
                    else
                    {
                        ssChkDecline.Checked = false;
                        ssChkAccept.Checked = false;
                        ssCertification.Visible = false;
                        authentication.Visible = true;
                        ssReview.Visible = true;
                        checkBoxSelected.Value = Reject;
                    }
                    break;
                case "animalssschkaccept":
                    if (animalsssChkAccept.Checked)
                    {
                        animalsssChkDecline.Checked = false;
                        animalsssCertification.Visible = false;
                        authentication.Visible = true;
                        animalsssReview.Visible = true;
                        checkBoxSelected.Value = Accept;
                    }
                    else
                    {
                        animalsssChkAccept.Checked = false;
                        animalsssChkDecline.Checked = false;
                        animalsssCertification.Visible = false;
                        authentication.Visible = true;
                        animalsssReview.Visible = true;
                        checkBoxSelected.Value = Reject;
                    }
                    break;
                case "animalspichkdecline":
                    animalspiChkAccept.Checked = false;
                    animalsPICertification.Visible = false;
                    authentication.Visible = true;
                    animalsPIReview.Visible = true;
                    checkBoxSelected.Value = Reject;
                    break;

                case "pichkdecline":
                    piChkAccept.Checked = false;
                    piCertification.Visible = false;
                    authentication.Visible = true;
                    piReview.Visible = true;
                    checkBoxSelected.Value = Reject;
                    break;

                case "sschkdecline":
                    ssChkAccept.Checked = false;
                    ssCertification.Visible = false;
                    authentication.Visible = true;
                    ssReview.Visible = true;
                    checkBoxSelected.Value = Reject;
                    break;
                case "updatedsschkdecline":
                    updatedssChkAccept.Checked = false;
                    ssCertification.Visible = false;
                    authentication.Visible = true;
                    updatedSSReview.Visible = true;
                    checkBoxSelected.Value = Reject;
                    break;
                case "animalssschkdecline":
                    animalsssChkAccept.Checked = false;
                    animalsssCertification.Visible = false;
                    authentication.Visible = true;
                    animalsssReview.Visible = true;
                    checkBoxSelected.Value = Reject;
                    break;
                case "updatedanimalssschkdecline":
                    updatedAnimalsssChkAccept.Checked = false;
                    animalsssCertification.Visible = false;
                    authentication.Visible = true;
                    updatedAnimalsssReview.Visible = true;
                    checkBoxSelected.Value = Reject;
                    break;

                case "spichkaccept":
                    if (spiChkAccept.Checked)
                    {
                        spiChkDecline.Checked = false;
                        spiCertification.Visible = true;
                        authentication.Visible = true;
                        spiReview.Visible = true;
                        checkBoxSelected.Value = Accept;
                    }
                    else
                    {
                        spiChkDecline.Checked = true;
                        spiCertification.Visible = false;
                        authentication.Visible = false;
                        spiReview.Visible = true;
                        checkBoxSelected.Value = Reject;
                    }
                    break;
                case "spichkdecline":
                    spiChkAccept.Checked = false;
                    spiCertification.Visible = false;
                    authentication.Visible = true;
                    spiReview.Visible = true;
                    checkBoxSelected.Value = Reject;
                    break;
                case "dcchkaccept":
                    if (dcChkAccept.Checked)
                    {
                        dcChkDecline.Checked = false;
                        dcCertification.Visible = true;
                        authentication.Visible = true;
                        dcReview.Visible = true;
                        checkBoxSelected.Value = Accept;
                    }
                    else
                    {
                        dcChkDecline.Checked = true;
                        dcCertification.Visible = false;
                        authentication.Visible = false;
                        dcReview.Visible = false;
                        checkBoxSelected.Value = Reject;
                    }
                    break;
                case "dcchkdecline":
                    dcChkAccept.Checked = false;
                    dcCertification.Visible = false;
                    authentication.Visible = true;
                    dcReview.Visible = true;
                    checkBoxSelected.Value = Reject;
                    break;
                case "srdcchkaccept":
                    if (srdcChkAccept.Checked)
                    {
                        srdcChkDecline.Checked = false;
                        srdcCertification.Visible = true;
                        authentication.Visible = true;
                        srdcReview.Visible = true;
                        checkBoxSelected.Value = Accept;
                    }
                    else
                    {
                        srdcChkDecline.Checked = true;
                        srdcCertification.Visible = false;
                        authentication.Visible = false;
                        srdcReview.Visible = false;
                        checkBoxSelected.Value = Reject;
                    }
                    break;
                case "srdcchkdecline":
                    srdcChkAccept.Checked = false;
                    srdcCertification.Visible = false;
                    authentication.Visible = true;
                    srdcReview.Visible = true;
                    checkBoxSelected.Value = Reject;
                    break;
                case "otherchkaccept":
                    if (otherChkAccept.Checked)
                    {
                        otherChkDecline.Checked = false;
                        authentication.Visible = true;
                        checkBoxSelected.Value = Accept;
                    }
                    else
                    {
                        authentication.Visible = false;
                    }
                    break;
                case "otherchkdecline":
                    if (otherChkDecline.Checked)
                    {
                        otherChkAccept.Checked = false;
                        authentication.Visible = true;
                        checkBoxSelected.Value = Reject;
                    }
                    else
                    {
                        authentication.Visible = false;
                    }
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Gets called when Authenticate button gets clicked. 
        /// This event authenticates user and if the user is authentic user then it enable Accept and Reject buttons. 
        /// Accept and Reject buttons are on Skelta control. So they are handled using Javascript.
        /// Also this events scrapes current HTML and sets it in Skelta variable to use by PDF creation step within Skelta.
        /// </summary>
        /// <param name="sender">Control which raised the event</param>
        /// <param name="e">Arguments associated with the event</param>
        protected void OnAuthenticate(object sender, EventArgs e)
        {
            try
            {
                //Do we need to extend this logic to LDAP??, I think so...
                string passwordEntry;
                //get the username data
                annunciator.Message = string.Empty;
                string userNameEntry = userName.Text.Trim().ToUpper();
                UserContext userContext = _cachePolicy.GetCachedObject(UserContext.CacheKey) as UserContext;
                if (userNameEntry != userContext.Username.Trim().ToUpper())
                {
                    annunciator.Message = "User name should match with the logged in user";
                    ClientScript.RegisterStartupScript(GetType(), "Disable", DisableScript);
                    return;
                }
                if (userNameEntry.Length == 0)
                {
                    throw new Exception("You must specify a user name");
                }

                if (Settings.GetKeyValue("ADAuthentication", false))
                {
                    passwordEntry = password.Text.Trim();
                    if (passwordEntry.Length == 0)
                    {
                        throw new Exception("You must specify a password");
                    }

                    Authenticator.ValidateCredentials(userNameEntry, passwordEntry);
                }

                /*at098 - SSO is not being used anywhere. In consultation with Srini and Bhaskar*/
                //((SSO)Application[Global.SSOKey]).Enter("Insight", userNameEntry);

                LoadUserContext(userNameEntry);

                switch (AreAllChecked())
                {
                        //All the required checkboxes are checked
                    case 3:
                        AcceptDeclineRelatedActivity();
                        string html = GetHTML();
                        //Add the returned HTML in the session so that workflow assembly will be 
                        //able to use it to create PDF out of it.									
                        //_cachePolicy.Cache(InsightUtility.Constants.WorkflowRevNCertPageKey,SerializeHTML(html));
                        SetWorkflowVariable(SerializeHTML(html));
                        //hasAuthenticated = true;
                        //annunciator.Message = "Authentication successful. Click the 'Accept' button at the bottom of the screen to complete.";
                        if (checkBoxSelected.Value == "Reject")
                        {
                            ClientScript.RegisterStartupScript(GetType(), Reject, RejectScript);
                            annunciator.Message = "Authentication successful. Click the 'Decline' button at the bottom of the screen to complete.";
                        }
                        else if (checkBoxSelected.Value == "Accept")
                        {
                            /*If animal process && animal related certification, Check if the user has Occ Health questionnaire 
                            * and the experience questionnaire within the last 3 years.If not the user should not be allowed to approve/reject the certifications*/
                            if (animalspiChkAccept.Checked || animalsssChkAccept.Checked || updatedAnimalsssChkAccept.Checked)
                            {
                                Person person = new Person(new NullCachePolicy());
                                person.Load(DataAccess.ConnectionString, userContext.PersonId);
                                if (Protocol != null)
                                {
                                    QuestionnaireAnswer occHealthQA = person.GetCertificationQuestionnaireAnswer(DataAccess.ConnectionString,
                                        FormsManagement.Constants.AnimalsQuestionnaireCommonFormNames.OccHealth);
                                    QuestionnaireAnswer experienceQA = person.GetCertificationQuestionnaireAnswer(DataAccess.ConnectionString, string.Format("{0} {1}", Protocol.SpeciesMeaning, "Qualifications and Experience"));
                                    if (!((person.IsOccHealthCertified || (occHealthQA != null)) &&
                                        (person.HasSpeciesExperience(Protocol.SpeciesMeaning) || (experienceQA != null))))
                                    {
                                        annunciator.Message = "Please complete the Occupational Health Survey / Qualifications & Experience by clicking the link above";
                                        ClientScript.RegisterStartupScript(GetType(), "Disable", DisableScript);
                                        return;
                                    }
                                }
                                else
                                {
                                    annunciator.Message = "Protocol out of context";
                                    ClientScript.RegisterStartupScript(GetType(), "Disable", DisableScript);
                                    return;
                                }
                            }
                            ClientScript.RegisterStartupScript(GetType(), Accept, AcceptScript);
                            annunciator.Message = "Authentication successful. Click the 'Accept' button at the bottom of the screen to complete.";
                        }
                        else
                        {
                            ClientScript.RegisterStartupScript(GetType(), "Enable", EnableScript);
                            annunciator.Message = "Authentication successful. Click the 'Accept' or 'Decline' button at the bottom of the screen to complete.";
                        }
                        break;
                    case 2: //Some of the checkbox(s) not checked					
                        annunciator.Message = "Please select all options";
                        ClientScript.RegisterStartupScript(GetType(), "Disable", DisableScript);
                        break;
                    case 1: //No checkbox availabale.
                        if ((checkBoxSelected.Value != string.Empty) && (checkBoxSelected.Value == Accept))
                        {
                            ClientScript.RegisterStartupScript(GetType(), Accept, AcceptScript);
                            annunciator.Message = "Authentication successful. Click the 'Accept' button at the bottom of the screen to complete.";
                        }
                        else if ((checkBoxSelected.Value != string.Empty) && (checkBoxSelected.Value == Reject))
                        {
                            ClientScript.RegisterStartupScript(GetType(), Reject, RejectScript);
                            annunciator.Message = "Authentication successful. Click the 'Decline' button at the bottom of the screen to complete.";
                        }
                        else
                        {
                            ClientScript.RegisterStartupScript(GetType(), "Enable", EnableScript);
                            annunciator.Message = "Authentication successful. Click the 'Accept' or 'Decline' button at the bottom of the screen to complete.";
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(GetType(), "Disable", DisableScript);
                ExceptionReport.WriteToEventLog("Error occured while authenticating the page.", ex, true, ExceptionReport.ReportType.Error);
                annunciator.Message = ex.Message;
            }
        }

        /// <summary>
        /// Gets called when Do not agree checkbox gets clicked.
        /// This checkboxes are available on departmentchair specific actions. This event makes sure that all positive checkboxes are unchecked when
        /// Do not agree checkbox gets selected.
        /// </summary>
        /// <param name="sender">Control which raised the event</param>
        /// <param name="e">Arguments associated with the event</param>
        private void OnDoNotAgreeCheckedChanged(object sender, EventArgs e)
        {
            if (((CheckBox) sender).Checked)
            {
                UncheckAll(true);
            }
            userName.Text = password.Text = annunciator.Message = string.Empty;
            ClientScript.RegisterStartupScript(GetType(), "disable", DisableScript);
        }

        /// <summary>
        /// Gets called when positive checkbox gets clicked.
        /// This event gets called only from Department chair specific positive checkboxes. This event makes sure that do not agree checkbox is unchecked.		
        /// </summary>
        /// <param name="sender">Control which raised the event</param>
        /// <param name="e">Arguments associated with the event</param>
        protected void OnPositiveCheckedChanged(object sender, EventArgs e)
        {
            if (((CheckBox) sender).Checked)
            {
                UncheckAll(false);
            }
        }

        #endregion

        #region Private Methods	

        /// <summary>
        /// This method uses Skelta workflow API and sets Skelta workflow variables.
        /// Skelta has it's own variables which can be defined in Skelta workflow manager
        /// screen at Start (very first) step. The scope of these variables is for individual
        /// workflow item. These variables are used to communicate between aplication and Skelta.
        /// In this method serialized xml string from the page gets saved in certificationXML variable in Skelta,
        /// which is used by PDFGeneration step. Also, server path is updated in this method.
        /// </summary>
        /// <param name="xml">Serialized xml string </param>
        private void SetWorkflowVariable(string xml)
        {
            string insightApplicationName = Settings.GetKeyValue("InsightApplicationName", string.Empty);
            // Config class object retrieves all the information of the application that is
            // configured in Workflow.NET.Config.Xml file.
            Config config = new Config(insightApplicationName);
            // Initializing DataHandler object which is used to do some database operations also to get the Workflow Context details.
            IDataHandler handler = DataHandlerFactory.GetDataHandler(config.DataSourceConnectionString, config.DataSourceType);
            // Initializing Runtime object to get the status of the workflow and actions
            RunTime runtime = RunTime.GetRuntime(Dns.GetHostName(), insightApplicationName, false);
            // Initializing the Workflow Context object to get the Workflow details like Application Name,
            // Workflow Name, Content and Variables value 
            // Here ExecutionId, Runtime object and Datahandler object should be passed.
            HumansWorkFlow wfHumans = new HumansWorkFlow(_cachePolicy);
            int executionId = wfHumans.GetWorkflowItemId(_pp.Id);
            Workflow.NET.Engine.Context context = Workflow.NET.Engine.Context.GetContext(executionId, runtime, handler);
            //The following two context variables are not used for PDF creation(It is not removed as it is refernced in workflows)
            context.Variables["certificationXML"].Value = "@" + xml;
            context.Variables["serverPath"].Value = Server.MapPath("");
            // Method that save the Variable and Content
            context.SaveVariables();
            context.Close();
            string activityName = (ssCertification.Visible) ? "sscertification" : (piCertification.Visible) ? "pisignoff" : (spiCertification.Visible) ? "sitepisignoff" : (srdcCertification.Visible) ? "srdcCertification" : (animalsPICertification.Visible) ? "pisignoff" : (animalsssCertification.Visible) ? "animalsssCertification" : "other";
            HumansWorkFlow.SaveCertifiedContent(executionId, _userContext.DomainUserId, activityName, "@" + xml, Server.MapPath(""));
        }

        /// <summary>
        /// This page is shown within Skelta control. This page shows different elemens depends on which role is calling it.
        /// By default nothing will be shown on the page. This method goes thr' different roles and sets the look of the 
        /// page accordingly. 
        /// Note: Need to improve two things in this method - 1. Currently ppType is not used. 
        /// This is going to be useful as we go for another processes. Currently there is only
        /// Initial Review. Next one is Amendment. This input parameter needs to be used.
        /// 2. Currently role is coming from querystring (big No No). This needs to be coming from Workflow assembly
        /// (Keeping commented code there so that nobody will forget this)
        /// </summary>
        /// <param name="protocolProcessType">Protocol Process Type (E.g. Initial Review, Amendment etc.)</param>
        /// <param name="role">Role (E.g. PI, SitePI etc.)</param>
        private void LoadPageElements(string protocolProcessType, string role)
        {
//			switch (protocolProcessType.ToUpper())
//			{
//				case "APPLICATION":
            //role = "SS_CERTIFICATION"; 				
            switch (role.ToUpper())
            {
                case "PRINCIPAL_INVESTIGATOR":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    piReview.Visible = true;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    ssCertification.Visible = false;
                    authentication.Visible = false;
                    submitter.Visible = false;
                    workflowAdmin.Visible = false;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    animalsssCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), "Disable", DisableScript);
                    break;
                case "ANIMALS_PRINCIPAL_INVESTIGATOR":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = true;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    ssCertification.Visible = false;
                    authentication.Visible = false;
                    submitter.Visible = false;
                    workflowAdmin.Visible = false;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), "Disable", DisableScript);
                    break;
                case "SITE_RESPONSIBLE_INVESTIGATOR":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = true;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    ssCertification.Visible = false;
                    authentication.Visible = false;
                    submitter.Visible = false;
                    workflowAdmin.Visible = false;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), "Disable", DisableScript);
                    break;
                case "DEPARTMENT_CHAIR":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = true;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    ssCertification.Visible = false;
                    submitter.Visible = false;
                    authentication.Visible = true;
                    workflowAdmin.Visible = false;
                    dcReview.Visible = true;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), "Disable", DisableScript);
                    break;
                case "IRBC":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = true;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    ssCertification.Visible = false;
                    submitter.Visible = false;
                    authentication.Visible = true;
                    workflowAdmin.Visible = false;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), "Enable", EnableScript);
                    break;
                case "SRDC":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = true;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    ssCertification.Visible = false;
                    submitter.Visible = false;
                    authentication.Visible = true;
                    workflowAdmin.Visible = false;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = true;
                    srdcCertification.Visible = false;
                    animalsssCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), "Disable", DisableScript);
                    break;
                case "SS_REVIEW":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = true;
                    ssCertification.Visible = false;
                    submitter.Visible = false;
                    authentication.Visible = false;
                    workflowAdmin.Visible = false;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), "Enable", EnableScript);
                    break;
                case "ANIMALS_SS_REVIEW":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    animalsssReview.Visible = true;
                    ssCertification.Visible = false;
                    submitter.Visible = false;
                    authentication.Visible = false;
                    workflowAdmin.Visible = false;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssCertification.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), "Disable", DisableScript);
                    break;
                case "SS_CERTIFICATION":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    submitter.Visible = false;
                    ssCertification.Visible = false;
                    authentication.Visible = true;
                    workflowAdmin.Visible = false;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = true;
                    ClientScript.RegisterStartupScript(GetType(), "Disable", DisableScript);
                    break;
                case "ANIMALS_SS_CERTIFICATION":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    submitter.Visible = false;
                    animalsssCertification.Visible = false;
                    ssCertification.Visible = false;
                    authentication.Visible = false;
                    workflowAdmin.Visible = false;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = true;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), "Disable", DisableScript);
                    break;
                case "SUBMITTER_REVIEW":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    ssCertification.Visible = false;
                    authentication.Visible = false;
                    submitter.Visible = true;
                    workflowAdmin.Visible = false;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), Reject, SubmitterRejectScript);
                    break;
                case "HRO":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = true;
                    ssReview.Visible = false;
                    ssCertification.Visible = false;
                    authentication.Visible = false;
                    submitter.Visible = false;
                    workflowAdmin.Visible = false;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), "Enable", EnableScript);
                    break;
                case "SRAC":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = true;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    ssCertification.Visible = false;
                    authentication.Visible = false;
                    submitter.Visible = false;
                    workflowAdmin.Visible = false;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), "Enable", EnableScript);
                    break;
                case "ANIMALS_COMPLIANCE":
                    compliancePerson.Visible = true;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    ssCertification.Visible = false;
                    authentication.Visible = false;
                    submitter.Visible = false;
                    workflowAdmin.Visible = false;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), "Enable", EnableScript);
                    break;
                case "ADMINISTRATOR":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    ssCertification.Visible = false;
                    authentication.Visible = false;
                    submitter.Visible = false;
                    workflowAdmin.Visible = true;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssReview.Visible = false;
                    animalsssCertification.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    break;
                case "NONIR_OTHER":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    ssCertification.Visible = false;
                    authentication.Visible = true;
                    submitter.Visible = false;
                    workflowAdmin.Visible = false;
                    spiReview.Visible = false;
                    other.Visible = false;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), "Disable", DisableScript);
                    break;
                case "NONIR_OTHER_PI":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    ssCertification.Visible = false;
                    authentication.Visible = true;
                    submitter.Visible = false;
                    workflowAdmin.Visible = false;
                    spiReview.Visible = false;
                    other_pi.Visible = true;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), "Disable", DisableScript);
                    break;
                case "PROTOCOL_ADMINISTRATOR":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    ssCertification.Visible = false;
                    authentication.Visible = false;
                    submitter.Visible = false;
                    workflowAdmin.Visible = false;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), "Enable", EnableScript);
                    break;
                case "ANCI_BOARD":
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    ssCertification.Visible = false;
                    authentication.Visible = false;
                    submitter.Visible = false;
                    workflowAdmin.Visible = false;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), "Enable", EnableScript);
                    break;
                default:
                    compliancePerson.Visible = false;
                    sRACIntakeUser.Visible = false;
                    animalsPIReview.Visible = false;
                    animalsPICertification.Visible = false;
                    piReview.Visible = false;
                    piCertification.Visible = false;
                    spiReview.Visible = false;
                    spiCertification.Visible = false;
                    dcCertification.Visible = false;
                    irbcCertification.Visible = false;
                    srdcCertification.Visible = false;
                    hroIntake.Visible = false;
                    ssReview.Visible = false;
                    ssCertification.Visible = false;
                    authentication.Visible = true;
                    submitter.Visible = false;
                    workflowAdmin.Visible = false;
                    dcReview.Visible = false;
                    dcCertification.Visible = false;
                    srdcReview.Visible = false;
                    srdcCertification.Visible = false;
                    animalsssCertification.Visible = false;
                    animalsssReview.Visible = false;
                    updatedAnimalsssReview.Visible = false;
                    updatedSSReview.Visible = false;
                    ClientScript.RegisterStartupScript(GetType(), "Enable", EnableScript);
                    break;
            }
            //break;
//				default:
//					break;
//			}
        }

        /// <summary>
        /// This method checks whether all the checkboxes are checked. Returns a integer value in the range 1 and 3 
        /// </summary>		
        /// <returns>1 - //No checkbox available
        ///			 2 - //Not all the checkbox are checked
        ///			 3 - //All the checkboxes are checked
        ///	</returns>
        private int AreAllChecked()
        {
            bool isChecked = true;
            bool isCheckBoxHit = false;

            for (int iCounter = 0; iCounter < outer.Controls.Count; ++iCounter)
            {
                if ((outer.Controls[iCounter].GetType().ToString() == "System.Web.UI.WebControls.Panel") && (outer.Controls[iCounter].Visible))
                {
                    Panel panelControl = (Panel) outer.Controls[iCounter];
                    for (int iPanelControlsCount = 0; iPanelControlsCount <= panelControl.Controls.Count - 1; ++iPanelControlsCount)
                    {
                        if (panelControl.Controls[iPanelControlsCount].GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTable")
                        {
                            HtmlTable TableControl = (HtmlTable) panelControl.Controls[iPanelControlsCount];
                            for (int iTableControlsCount = 0; iTableControlsCount <= TableControl.Controls.Count - 1; ++iTableControlsCount)
                            {
                                try
                                {
                                    //The try-catch for this block will suppress the exception that will be generated if the checkbox control is not found
                                    if (TableControl.Controls[iTableControlsCount].Controls[0].Controls[1].GetType().ToString() == "System.Web.UI.WebControls.CheckBox")
                                    {
                                        CheckBox checkBoxControl = (CheckBox) TableControl.Controls[iTableControlsCount].Controls[0].Controls[1];
                                        isCheckBoxHit = true;
                                        if ((checkBoxControl.ID.IndexOf(Accept) < 0) && (checkBoxControl.ID.IndexOf("Decline") < 0) && (checkBoxControl.ID.IndexOf("DoNotAgree") < 0))
                                        {
                                            if (!checkBoxControl.Checked)
                                            {
                                                isChecked = false;
                                            }
                                        }
                                        if (!isChecked)
                                        {
                                            if (checkBoxControl.ID.IndexOf("DoNotAgree") > 0)
                                            {
                                                if (checkBoxControl.Checked)
                                                {
                                                    isChecked = true;
                                                    break; //If Do not agree checkbox is selected get out with true as we know all other checkboxes don't matter
                                                }
                                                break;
                                            }
                                        }
                                    }
                                }
                                catch
                                {
                                    /*table.Controls[0].Controls[1] is avaialable only when the checkbox is present and hence the exception raised by not having the checkbox should be neglected */
                                    //Continue to go through the htmltable to find the checkBox
                                    continue;
                                }
                            }
                        }
                    }
                }
            }
            int returnVal;
            if (!isCheckBoxHit)
            {
                returnVal = 1; //No checkbox available
            }
            else if (!isChecked)
            {
                returnVal = 2; //Not all the checkbox are checked
            }
            else
            {
                returnVal = 3; //All the checkboxes are checked
            }
            return returnVal;
        }

        /// <summary>
        /// Loads a UserContext object based on the passed in domain username,
        /// checks that the user is active  If the user is inactive a system 
        /// exception is thrown.
        /// </summary>
        /// <param name="userName">Domain user's username</param>
        private void LoadUserContext(string userName)
        {
            if (_userContext == null)
            {
                _userContext = new UserContext(_cachePolicy);
                string connectionString = DataAccess.GetConnectionString();

                //get the user's context			
                _userContext.Load(connectionString, userName);

                if (_userContext.IsActiveBitMask <= 1)
                {
                    throw new Exception("The user is inactive.");
                }
            }
        }

        /// <summary>
        /// This method gets the latest HTML rendered on the page from hidden variable and cleans it and returns 
        /// it to the calle.
        /// This HTML is used to generate PDF in next step of Workflow.
        /// </summary>	
        /// <returns>Cleaned HTML string</returns> 
        private string GetHTML()
        {
            string htmlValue;
            try
            {
                //htmlValue = (Request.Form[HiddenValueFiled]).ToLower();
                //Need to take care of htis because some browsers are giving all HTML tags in lower or some upper case. 
                //Making Tolower to the whole HTML is giving problem in generated PDF.
                htmlValue = Request.Form[HiddenValueFiled];
                int startIndex1 = htmlValue.ToUpper().IndexOf(StartTag1Upper);
                int startIndex2 = htmlValue.ToUpper().IndexOf(StartTag2Upper);

                if (startIndex1 > 0)
                {
                    htmlValue = htmlValue.Substring(startIndex1);
                }
                else if (startIndex2 > 0)
                {
                    htmlValue = htmlValue.Substring(startIndex2);
                }

                int endIndex = htmlValue.ToUpper().IndexOf(EndTagUpper) + 8;

                htmlValue = htmlValue.Substring(0, endIndex);
                htmlValue = htmlValue.Replace(Crlf, "");
                htmlValue = htmlValue.Replace(Tab, "");
            }
            catch (Exception ex)
            {
                ExceptionReport.WriteToEventLog("Problem in capturing HTML from the page.", ex, true, ExceptionReport.ReportType.Error);
                throw new Exception("Problem in capturing HTML from the page.");
            }
            return htmlValue;
        }

        /// <summary>
        /// This method converts HTML to XML and then serializes it.
        /// This serialized XML is used for PDF generation using third party tool, IBEX.
        /// </summary>
        /// <param name="html">HTML string to convert and serialize</param>
        /// <returns>Serialized XML string</returns> 
        private string SerializeHTML(string html)
        {
            int htmlLength = html.Length;
            int count = 0;
            ArrayList staffConsentItems = new ArrayList();
            while (count <= htmlLength - 1)
            {
                int tdStartIndex = html.ToUpper().IndexOf(TDStart);
                if (tdStartIndex > 0)
                {

                    StaffConsentItem staffConsetItem = new StaffConsentItem();
                    int tdEndIndex = html.ToUpper().IndexOf(TDEnd, tdStartIndex) - tdStartIndex;
                    string element = html.Substring(tdStartIndex + TDStart.Length, tdEndIndex - 4);
                    if ((element.ToUpper().IndexOf(BoldEnd, 0)) > 0)
                    {
                        staffConsetItem.Type = Heading;
                        staffConsetItem.Content = element.Substring(3, element.ToUpper().IndexOf(BoldEnd) - 3);
                        staffConsetItem.Status = NotApplicable;
                    }
                    else if (element.ToUpper().IndexOf(Input) > 0)
                    {
                        int typeIndex = element.ToUpper().IndexOf(ElementType, 0);
                        if (typeIndex > 0)
                        {
                            string type = element.Substring(typeIndex + (ElementType).Length, element.IndexOf(" ", typeIndex) - typeIndex - (ElementType).Length);
                            if (type.IndexOf("\"") >= 0) //ex: <...type="checkbox">
                            {
                                type = type.Substring(1, type.IndexOf("\"", 2) - 1);
                            }
                            else if (type.IndexOf(">") > 0) //ex: <...type=checkbox>
                            {
                                type = type.Substring(0, type.IndexOf(">") - 1);
                            }
                            staffConsetItem.Type = type.ToUpper();

                            string content;

                            if (element.ToUpper().IndexOf(LabelStart) > 0)
                            {
                                int startIndex = element.ToUpper().IndexOf(TagEndChar, element.ToUpper().IndexOf(LabelStart, 0)) + 1;
                                int endIndex = element.ToUpper().IndexOf(TagEnd, startIndex);
                                content = element.Substring(startIndex, endIndex - startIndex);
                            }
                            else
                            {
                                //Remove all anchor tags if there are any...							
                                //Before clean all tags see if it is a checkbox
                                if (element.ToUpper().IndexOf(Checked, 0) > 0)
                                {
                                    staffConsetItem.Status = Checked;
                                }
                                element = Regex.Replace(element, "<[^>]+>", string.Empty);
                                content = Regex.Replace(element, "</[^>]+>", string.Empty);
                            }

                            staffConsetItem.Content = content;
                            if (element.ToUpper().IndexOf(Checked, 0) > 0)
                            {
                                staffConsetItem.Status = Checked;
                            }
                        }
                    }
                    else
                    {
                        //Remove all anchor tags if there are any...	
                        element = Regex.Replace(element, "<[^>]+>", string.Empty);

                        staffConsetItem.Type = TextType;
                        staffConsetItem.Content = element;
                        staffConsetItem.Status = NotApplicable;
                    }
                    count = count + tdEndIndex;
                    html = html.Substring(tdStartIndex + TDStart.Length + tdEndIndex);
                    staffConsentItems.Add(staffConsetItem);
                }
                else
                {
                    break;
                }
            }

            string logoPath = Server.MapPath("");
            logoPath = logoPath.Substring(0, logoPath.LastIndexOf("\\") + 1) + Settings.GetKeyValue("PartnersLogo", "");

            return Serialize(_cachePolicy, logoPath, staffConsentItems);
        }

        /// <summary>
        /// This method will see whether the activity is related to Accept/Decline related. If that is the case then it will also check whether
        /// the user is agreeing to the statements by clicking poisitive checkboxes or not agreeing by clicking do not agree checkbox.
        /// Is it is positive then it makes accept button enable otherwise makes reject button enabled. This logic is required for
        /// PI, Department Chair Certification and Site-Responsible Department Chair certification.
        /// </summary>				
        /// <returns>true - if Accept/Decline related activity
        ///			 false - if other than department related activity</returns> 
        private bool AcceptDeclineRelatedActivity()
        {
            bool activeDeclineActivity = false;
            if ((dcCertification.Visible) || (srdcCertification.Visible))
            {
                activeDeclineActivity = true;
                if ((srdcChkDecline.Checked) || (dcChkDecline.Checked))
                {
                    ClientScript.RegisterStartupScript(GetType(), Reject, RejectScript);
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), Accept, AcceptScript);
                }
            }
            else if (piReview.Visible)
            {
                activeDeclineActivity = true;
                if (piChkAccept.Checked)
                {
                    ClientScript.RegisterStartupScript(GetType(), Accept, AcceptScript);
                }
                else if (piChkDecline.Checked)
                {
                    ClientScript.RegisterStartupScript(GetType(), Reject, RejectScript);
                }
            }
            else if (animalsPIReview.Visible)
            {
                activeDeclineActivity = true;
                if (animalspiChkAccept.Checked)
                {
                    ClientScript.RegisterStartupScript(GetType(), Accept, AcceptScript);
                }
                else if (animalspiChkDecline.Checked)
                {
                    ClientScript.RegisterStartupScript(GetType(), Reject, RejectScript);
                }
            }
            else if (spiCertification.Visible)
            {
                activeDeclineActivity = true;
                if (spiChkAccept.Checked)
                {
                    ClientScript.RegisterStartupScript(GetType(), Accept, AcceptScript);
                }
                else if (spiChkDecline.Checked)
                {
                    ClientScript.RegisterStartupScript(GetType(), Reject, RejectScript);
                }
            }
            else if (ssCertification.Visible)
            {
                activeDeclineActivity = true;
                if (ssConflictOfInterest.Checked && ssFamiliar.Checked && ssProcessCredentials.Checked && ssProtectionEd.Checked)
                {
                    ClientScript.RegisterStartupScript(GetType(), Accept, AcceptScript);
                }
                else if (!ssConflictOfInterest.Checked || !ssFamiliar.Checked || !ssProcessCredentials.Checked || ssProtectionEd.Checked)
                {
                    ClientScript.RegisterStartupScript(GetType(), Reject, RejectScript);
                }
            }
            else if (animalsssCertification.Visible)
            {
                activeDeclineActivity = true;
                if (ssCertiAnimalsHandling.Checked && ssCertiAnimalsPolicies.Checked && ssCertiFacilities.Checked)
                {
                    ClientScript.RegisterStartupScript(GetType(), Accept, AcceptScript);
                }
                else if (!ssCertiAnimalsHandling.Checked || !ssCertiAnimalsPolicies.Checked || !ssCertiFacilities.Checked)
                {
                    ClientScript.RegisterStartupScript(GetType(), Reject, RejectScript);
                }
            }
            return activeDeclineActivity;
        }

        /// <summary>
        /// This method unchecks all requested checkboxes
        /// </summary>		
        /// <param name="positiveCheckBoxes">If uncheck all positive checkboxes then true elase false</param>
        private void UncheckAll(bool positiveCheckBoxes)
        {
            bool doneWithIt = false;
            for (int iCounter = 0; iCounter <= outer.Controls.Count - 1; ++iCounter)
            {
                if ((outer.Controls[iCounter].GetType().ToString() == "System.Web.UI.WebControls.Panel") && (outer.Controls[iCounter].Visible))
                {
                    Panel panelControl = (Panel) outer.Controls[iCounter];
                    for (int iPanelControlsCount = 0; iPanelControlsCount <= panelControl.Controls.Count - 1; ++iPanelControlsCount)
                    {
                        if (panelControl.Controls[iPanelControlsCount].GetType().ToString() == "System.Web.UI.WebControls.CheckBox")
                        {
                            CheckBox checkBoxControl = (CheckBox) panelControl.Controls[iPanelControlsCount];
                            if (positiveCheckBoxes)
                            {
                                if (checkBoxControl.ID.IndexOf("DoNotAgree") < 0)
                                {
                                    checkBoxControl.Checked = false;
                                }
                            }
                            else
                            {
                                if (checkBoxControl.ID.IndexOf("DoNotAgree") > 0)
                                {
                                    checkBoxControl.Checked = false;
                                    doneWithIt = true;
                                    break; //There is only one of this kind so once this is done get out
                                }
                            }
                        }
                    }
                }
                if (doneWithIt)
                {
                    break;
                }
            }
        }

        /// <summary>
        /// This method serializes Certification form html from workFlow in to XML as string Output.		
        /// </summary>
        /// <param name="cachePolicy">CachePolicy</param>		
        /// <param name="logoPath">Insight logo Path</param>	
        /// <param name="staffConsentItems">ArrayList of staffConsent Items</param>																																	
        /// <returns>returns the serialized string to the caller</returns>
        private string Serialize(ICachePolicy cachePolicy, string logoPath, ArrayList staffConsentItems)
        {
            string serializedString = string.Empty;
            if (_pp.Id != 0)
            {
                string connection = DataAccess.GetConnectionString();
                StaffConsentItemCollection staffConsentCollection = new StaffConsentItemCollection();
                DataTable dt = DataAccess.GetDataTable(connection,
                    "GetPDFHeaderDetails",
                    DataAccess.BuildSqlParameterArray(
                        "@protocolProcessId", SqlDbType.Int, _pp.Id));
                DataRow dr = dt.Rows[0];

                staffConsentCollection.Title = (DBNull.Value == dr["Title"]) ? string.Empty : dr["Title"].ToString();
                staffConsentCollection.PIName = (DBNull.Value == dr["PIName"]) ? string.Empty : dr["PIName"].ToString();
                staffConsentCollection.ProtocolNumber = (DBNull.Value == dr["ProtocolNo"]) ? string.Empty : dr["ProtocolNo"].ToString();
                staffConsentCollection.Sponsor = (DBNull.Value == dr["Sponsor"]) ? string.Empty : dr["Sponsor"].ToString();
                staffConsentCollection.ApprovedDate = (DBNull.Value == dr["ApprovedDate"]) ? string.Empty : ((DateTime) dr["Today"]).ToString("MMMM dd, yyyy");
                staffConsentCollection.ExpiryDate = (DBNull.Value == dr["ExpiryDate"]) ? string.Empty : ((DateTime) dr["ExpiryDate"]).ToString("MMMM dd, yyyy");
                staffConsentCollection.Today = (DBNull.Value == dr["Today"]) ? string.Empty : ((DateTime) dr["Today"]).ToString("MMMM dd, yyyy");
                staffConsentCollection.LogoLocation = logoPath;
                staffConsentCollection.Type = (DBNull.Value == dr["Type"]) ? string.Empty : dr["Type"].ToString();
                staffConsentCollection.ApprovedBy = _userContext.FirstName + " " + _userContext.LastName;
                staffConsentCollection.Species = (DBNull.Value == dr["SpeciesCode"]) ? string.Empty : dr["SpeciesCode"].ToString();
                if (staffConsentItems.Count > 0)
                {
                    staffConsentCollection.StaffConsentItems = staffConsentItems;
                }
                serializedString = QuestionnaireSerializer.Serialize(staffConsentCollection);
            }
            return serializedString;
        }

        #endregion

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
            _cachePolicy = (Policy.ICachePolicy) Session[Policy.Names.CachePolicyKey];
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.OnPageLoad);
        }

        #endregion
    }
}